/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.store.client.subcomponents;

import com.elf.gwt.common.client.callback.SubComponentCallback;
import com.elf.gwt.common.client.utils.CommonUtils;
import com.elf.gwt.store.client.GWTCommerceConstants;
import com.elf.gwt.store.client.StoreComponentController;
import com.elf.gwt.store.client.components.AccountComponent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  Subcomponent for loging into application
 */
public class AccountPasswordForgottenSubComponent extends StoreSubComponent
{
	public TextBox emailBox;
	protected InlineHTML warningLabel;

	/**
	 * Constructor
	 */
	public AccountPasswordForgottenSubComponent(
			StoreComponentController controller, 
			SubComponentCallback callback,
			String emailText)
	{
		super(controller, "AccountComponent", AccountComponent.PasswordForgotten, callback);
		
		if (accountComponent == null) accountComponent = (AccountComponent)controller.getComponent("AccountComponent");

		GWTCommerceConstants constants = controller.getStoreConstants();
		thisComponent.add(new InlineHTML("<br/>"));
		thisComponent.add(new InlineHTML("If you've forgotten your password, enter your e-mail address below<br/>and we\'ll send you an e-mail message containing your new password."));
		thisComponent.add(new InlineHTML("<br/>"));
		Grid forgottenGrid = new Grid(1,3);
		forgottenGrid.setWidth("100%");
		forgottenGrid.setStyleName(handle + "SubPanel");
		forgottenGrid.getColumnFormatter().setStyleName(0, handle + "SubPanelCol0");
		thisComponent.add(forgottenGrid);

		emailBox = new TextBox(); 
		emailBox.setText(emailText);
		emailBox.addKeyUpHandler(new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					AccountPasswordForgottenSubComponent comp = (AccountPasswordForgottenSubComponent)thisComponent;
					comp.submit();
				}
			}
		});
		CommonUtils.addGridWidget(handle, 0, emailBox, forgottenGrid, "email_address", constants.AccountComponentEmailAddress(), "");

		HorizontalPanel hpanel = new HorizontalPanel();

		Button editButton = new Button(controller.getCommonConstants().Continue(), 
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						AccountPasswordForgottenSubComponent comp = (AccountPasswordForgottenSubComponent)thisComponent;
						comp.submit();
					}}
		);
		hpanel.add(editButton);

		Button cancelButton = new Button(controller.getCommonConstants().GoBack(), 
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						getCallback().onNavigate(thisComponent);
					}}
		);
		hpanel.add(cancelButton);
		hpanel.setCellHorizontalAlignment(cancelButton, HasHorizontalAlignment.ALIGN_RIGHT);

		hpanel.setWidth("100%");
		thisComponent.add(hpanel);

		warningLabel = new InlineHTML();
		warningLabel.setStyleName("WarningLabel");
		warningLabel.addStyleName(handle + "WarningLabel");
		thisComponent.add(warningLabel);
	}

	/**
	 * build
	 */
	public void build() 
	{
	}

	/**
	 * validate information prior to form submission
	 */
	public void submit() 
	{
		GWTCommerceConstants constants = controller.getStoreConstants();

		StringBuffer warningText = new StringBuffer();

		warningText = CommonUtils.lengthWarning(controller.getCommonConstants(), warningText, emailBox, 
				constants.AccountComponentEmailAddress(), accountComponent.getAccountMetaModel().getEntryEmailAddressLengthMin());

		if (!CommonUtils.validateEmailFormat(emailBox.getText()))
		{
			warningText.append("<br/>");
			warningText.append(constants.AccountComponentEmailAddressCheckError());
		}

		if (warningText.length() == 0) 
		{
			accountComponent.getAccountModel().setEmailAddress(emailBox.getText());
			getCallback().onSuccess(thisComponent);
		}
		else
		{
			String str = warningText.toString();
			str.replace("\n", "<br/>");
			str = str + "<br/>";
			warningLabel.setHTML(str);
		}
	}
}
